package org.chris.zookeeper;

import com.alibaba.fastjson.JSON;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Description: Zookeeper<br/>
 * @version V1.0  by 石冬冬-Heil Hitler on  2017/7/4 14:27
 */
public class ZookeeperMain {
    private static final int timeOut = 9999999;
    static ZooKeeper zk;
    static Watcher watcher = new Watcher() {
        @Override
        public void process(WatchedEvent watchedEvent) {
            System.out.println("输出watcher:" + JSON.toJSON(watchedEvent.toString()));
        }
    };
    static{
        try {
            zk = new ZooKeeper("192.168.207.128:2181",timeOut,watcher);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建节点
     * @param nodes
     */
    public void createNode(List<String> nodes){
        try {
            for(String node : nodes){
                String nodePath = "/zookeeper/" + node;
                if(null == zk.exists(nodePath,false)){
                    zk.create(nodePath,node.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                }else{
                    System.out.println("存在该节点忽略创建" + node);
                }
            }
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询节点
     * @param path
     */
    public void queryNodes(String path){
        try {
            List<String> nodes = zk.getChildren(path, new Watcher() {
                @Override
                public void process(WatchedEvent watchedEvent) {
                    System.out.println("查询节点输出watcher:" + JSON.toJSON(watchedEvent.toString()));
                }
            });
            System.out.println(JSON.toJSON(nodes));

            for(String node : nodes){
                String nodePath = path + "/" + node;
                byte[] data = zk.getData(nodePath, new Watcher() {
                    @Override
                    public void process(WatchedEvent watchedEvent) {
                        System.out.println("获取节点data时watcher:" + JSON.toJSON(watchedEvent.toString()));
                    }
                },new Stat());
                System.out.println(MessageFormat.format("zookeeper节点路径{0}的data={1}",nodePath,new String(data)));
            }
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        final ZookeeperMain main = new ZookeeperMain();
        //初始化5个节点
        main.createNode(new ArrayList<String>(){{
            add("test1");add("test2");add("test3");add("test4");add("test5");
        }});
        //启用一个timer，每隔3秒输出当前节点的相关信息
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                main.queryNodes("/zookeeper");
            }
        },0,3000);
    }
}
